$(function(){
	//1,3,6小时降水
	var colorCode=["#A9FFFD","#2DD4FD","#2DA7FF","#2D7EFF","#51A82D","#4AFF2D","#FFFD2D","#FFD12F",
				"#FFA82D","#FF2D2D","#CD2F30","#AA2D2D","#AC2DAC","#DA2DDE","#F02DFF"];
	var cThree=["0.1","2","5","10","15","25","30","40","50","60","70","80","90","100","110"];
	var cCNTwelve=["0.1","2","5","10","15","25","30","40","50","60","80","100","120","140","150"];
	var cCTwentyfour=["0.1","2","5","10","15","25","35","50","70","100","120","150","170","200","230","250"];
 	var map = QXMap.Map('container', {
	        zoom:10,
	        center: [120.260,30.086],
	        maptype: QMAP_NORMAL_MAP
	    });
 	var  productcategory={
			title:"OCF",
			type:"pr03",
			urls:"10.135.30.116:8080",
			nav:"NAFP_FOR_FTM_OCF_ZJ",
			userid:"3小时降水预报",
			num:"3",
			timetype:"H",
		};
 	var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
				+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
				+'&time='+'20171019200000'+'&timetype='+productcategory.timetype+'&'
				+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
	
	 
	 //绘制萧山边界
		$.ajax({
			url:"./data/萧山_all.xml",
			data:"json",
			success:function(data){
				var points=$(data).find("Area").attr("Coords").split(",")
				for(var i=0,arr=[];i<points.length;i++){
					if(i%2==0){
						arr.push({lng:points[i],lat:points[i+1]})
					}
				}
				var polylineOptions =
				   {
				       color: 'red', 
				       weight: 3,
				       fillColor: 'red',
				       opacity: 0.7,
				       fillopacity: 0.8,
				       isfill: false
				   }
    			var polyline = QXMap.PolyLine(arr, polylineOptions);
    			map.addVectorLayer(polyline);
    			//map.setCenter({lng:120.260,lat:30.186})

			}
		})

	//1小时降雨
	function oneRain(){
		
		var time=oneTime();
		$(".pubTime .w180").val(time)
		var hour=time.slice(11,13);
		var day=time.slice(8,10)
		time=time.replace(/\//g,"").replace(" ","").replace(/:/g,"")+"00";
		console.log(time)
		var productcategory={
			title:"shengtai",
			type:"apcpsfc",
			nav:"NAFP_FOR_FTM_ST24H_ZJ",
			userid:"小时降水预报",
			num:"1",
			timetype:"H",
			year:time,
			hour:"1"
		};
		grid_controller(0.0,productcategory)
		addColorCode('1小时降水预报',colorCode,cThree);
		var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
				+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
				+'&time='+time+'&timetype='+productcategory.timetype+'&'
				+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
		$.ajax({
			url:ajaxUrl,
			type:"get",
			success:function(data){
				drawAxisOne(parseInt(hour),1,data.DS);
				removeAxis(productcategory)
				console.log(data.DS)
			}
		})
	}
	oneRain()
	//3小时降雨函数
	function threeRain(){
		var time =threeTime();
		console.log(time)
		$(".pubTime .w180").val(time)
		var hour=time.slice(11,13);
		
		
		var day=time.slice(8,10)
		time=time.replace(/\//g,"");
		//time=time.replace(/:/g,"")+"00";
		var t1=(parseInt(hour)==20)?'12':"00";
		time=time.slice(0,8)+t1+'0000'
		console.log(t1,time)
		var productcategory={
			title:"OCF",
			type:"pr03",
			urls:"10.135.30.116:8080",
			nav:"NAFP_FOR_FTM_OCF_ZJ",
			userid:"3小时降水预报",
			num:"3",
			timetype:"H",
			year:time
		}
		grid_controller(0.0,productcategory)
		var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
			+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
			+'&time='+time+'&timetype='+productcategory.timetype+'&'
			+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
		console.log(hour,day)
		addColorCode('3小时降水预报',colorCode,cThree);
		
		$.ajax({
			url:ajaxUrl,
			type:"get",
			success:function(data){
				drawAxisThree(parseInt(hour),3,day,5,2,data.DS);
				removeAxis(productcategory)
				console.log(data)
			}
		})
	}
	//threeRain()
	//6小时降雨函数
	function sixRain(){
		var time =threeTime();

		$(".pubTime .w180").val(time)
		var hour=time.slice(11,13);
		var day=time.slice(8,10)
		time=time.replace(/\//g,"");
		// time=time.replace(/:/g,"")+"00";
		var t1=(parseInt(hour)==20)?'12':"00";
		time=time.slice(0,8)+t1+'0000'
		var productcategory={
			title:"OCF",
			type:"pr06",
			urls:"10.135.30.116:8080",
			nav:"NAFP_FOR_FTM_OCF_ZJ",
			userid:"6小时降水预报",
			num:"6",
			timetype:"H",
		};

		grid_controller(0.0,productcategory)
		var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
			+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
			+'&time='+time+'&timetype='+productcategory.timetype+'&'
			+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';


		console.log(hour,day)
		addColorCode('6小时降水预报',colorCode,cThree);
		$.ajax({
			url:ajaxUrl,
			type:"get",
			success:function(data){
				drawAxisSix(parseInt(hour),6,day,8,2,data.DS);
				removeAxis(productcategory)
				console.log("data",data.DS)
			}
		})
	}
	//sixRain()
	//12小时降雨函数
	function twelveRain(){
		var time =threeTime();
		$(".pubTime .w180").val(time)
		var hour=time.slice(11,13);
		var day=time.slice(8,10)
			time=time.replace(/\//g,"");
			// time=time.replace(/:/g,"")+"00";
			var t1=(parseInt(hour)==20)?'12':"00";
				time=time.slice(0,8)+t1+'0000'
		var productcategory={
				title:"OCF",
				type:"pr12",
				urls:"10.135.30.116:8080",
				nav:"NAFP_FOR_FTM_OCF_ZJ",
				userid:"12小时降水预报",
				num:"12",
				timetype:"H",
				year:time,
				hour:"12"
			};
			grid_controller(0.0,productcategory)
			addColorCode('12小时降水预报',colorCode,cCNTwelve);
		var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
				+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
				+'&time='+time+'&timetype='+productcategory.timetype+'&'
				+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
			$.ajax({
					url:ajaxUrl,
					type:"get",
				success:function(data){
					drawAxisThree(parseInt(hour),12,day,20,8,data.DS);
					removeAxis(productcategory)
					console.log(data)
				}
			})	
	}
	//twelveRain()
	//24小时降雨函数
	function twentyFourRain(){
		var time =threeTime();
		$(".pubTime .w180").val(time)
		var hour=time.slice(11,13);
		var day=time.slice(8,10)
			time=time.replace(/\//g,"");
			// time=time.replace(/:/g,"")+"00";
			var t1=(parseInt(hour)==20)?'12':"00";
				time=time.slice(0,8)+t1+'0000'
		var productcategory={
				title:"OCF",
				type:"pr24",
				urls:"10.135.30.116:8080",
				nav:"NAFP_FOR_FTM_OCF_ZJ",
				userid:"24小时降水预报",
				num:"24",
				timetype:"H",
				year:time,
				hour:"24"
			};
			grid_controller(0.0,productcategory)

			addColorCode('24小时降水预报',colorCode,cCTwentyfour);
		// var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
		// 		+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
		// 		+'&time='+time+'&timetype='+productcategory.timetype+'&'
		// 		+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';

		// var jaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
		// 		+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
		// 		+'&time='+time+'&timetype='+productcategory.timetype+'&'
		// 		+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
				// var jaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/NAFP_FOR_FTM_OCF_ZJ/getContourEleValidtimeListByTime?userid=zjwlzx&pwd=123&productcategory=pr24&fcstlevel=0&time=20171024000000&timetype=H&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json'
				var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/'+productcategory.nav+'/getContourEleValidtimeListByTime'
				+'?userid=zjwlzx&pwd=123&productcategory='+productcategory.type+'&fcstlevel=0'
				+'&time='+time+'&timetype='+productcategory.timetype+'&'
				+'admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
			$.ajax({
					url:ajaxUrl,
					type:"get",
				success:function(data){
					drawAxistwentyFour(data.DS)
					removeAxis(productcategory)
					console.log(data)
				}
			})				
	}
	//twentyFourRain()
	//获得当前时间函数
	function getTime(){
		var date = new Date();
		var Day = date.getDate()>9?date.getDate():"0"+date.getDate();
		var Year = date.getFullYear();
		var Mouth = date.getMonth()>=9?date.getMonth()+1:"0"+(date.getMonth()+1);
		var hour=date.getHours()>9?date.getHours():"0"+date.getHours();
		var min=date.getMinutes()>9?date.getMinutes():"0"+date.getMinutes();
		var end=Year+"/"+Mouth+"/"+Day+" "+hour+":"+"00";
		return end;
	};
	//3小时降雨发布函数
	function threeTime(){
		//var nowTime=getTime();
		//var hour=nowTime.slice(11,13);
		var date=new Date();
		var hour=date.getHours();
		if(parseInt(hour)<6){
			date.setDate(date.getDate()-1);
			hour="08";
		}else if(parseInt(hour)>=6&&parseInt(hour)<14){
			date.setDate(date.getDate()-1);
			hour="20";
		}else{
			hour="08";
		}
		return date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate()+"/"+hour+":00";
		//console.log(hour)
	}	
	// 增加色标
	function addColorCode(title,colorCode,cc){
		var html=' <p>'+title+'</p><ul>'
		for(var i=0;i<colorCode.length;i++){
			html+='<li>'+
                            '<span  style="background:'+colorCode[i]+'"></span>'+
                            '<span>'+cc[i]+'</span>'+
                    '</li>'
		}
		html+="</ul>"
		$(".colorCode").html(html)                 
	}
	
	//时间切换函数
	function removeAxis(productcategory){
		var index=0;
		$(".changeTime").unbind();
		$(".changeTime").on("click","button",function(e){
			e.preventDefault();
			var len=$(".changeTime ul li")
			if($(this).hasClass("changeNext")){
				$(this).siblings("button").prop("disabled",false)
				len.eq(index).addClass("displayNone").removeClass("displayBlock")
				len.eq(++index).addClass("active").siblings(".active").removeClass("active")
				productcategory.year=len.eq(index).attr('year');
				productcategory.hour=len.eq(index).attr("hour");
				grid_controller(0,productcategory)
				if(len.length-index<=8){$(this).prop("disabled",true)}
				console.log("changeNext",len.length)
			}else{
				--index
				$(this).siblings("button").prop("disabled",false)
				len.eq(index)
				len.eq(index).removeClass("displayNone").addClass("displayBlock").addClass("active").siblings(".active").removeClass("active")
				productcategory.year=len.eq(index).attr('year');
				productcategory.hour=len.eq(index).attr("hour");
				grid_controller(0,productcategory)
				if(index==0){
					$(this).prop("disabled",true);
				}
				console.log(productcategory)
			}
		})
		$(".changeTime").on("mouseenter","li",function(){
			$(this).addClass("active").siblings(".active").removeClass("active");
			productcategory.year=$(this).attr('year');
			productcategory.hour=$(this).attr("hour");
			grid_controller(0,productcategory)
			console.log($(this).index())
		})
		$(".changeTime").on("mouseleave","li",function(){
			// $(this).removeClass("active");
		})
	}
	//绘制1小时底部时间轴
	function drawAxisOne(initTime,step,data){
		var html="";
		var j=1;
		var date=new Date();
		for(var i=0;i<data.length;i++){
			var time=initTime+j*step;
			j++
			html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
			'<span class="upRect"></span>'+
			'<span>'+(time>23?00:time)+':00</span>'
			if(time>23){
				initTime=1;
				j=0;
				date.setDate(date.getDate()+1)
				html+='<i ></i>'+
				'<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
			}
			html+='</li>'
		}
		$(".changeTime ul").html(html)
	}
	
	//绘制3小时底部时间轴
	function drawAxisThree(initTime,step,day,endT,num,data){
		console.log(endT)
		var html="";
		var j=1;
		var time
		var date=new Date();
			date.setDate(day);	
		for(var i=0;i<data.length;i++){
			time=initTime+j*step;

			j++
			html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
			'<span class="upRect"></span>'+
			'<span>'+(time>23?num:time)+':00</span>'
			if(time>23){
				initTime=endT;
				j=0;
				date.setDate(date.getDate()+1)
				html+='<i ></i>'+
				'<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
			}
			html+='</li>'
		}
		$(".changeTime ul").html(html)
	}
	function drawAxisSix(initTime,step,day,endT,num,data){
		console.log(endT)
		var html="";
		var j=1;
		var time
		var date=new Date();
			date.setDate(day);	
		for(var i=0;i<data.length;i++){
			time=initTime+j*step;
			if(data[i].hours%6==0){
				j++
				html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
					'<span class="upRect"></span>'+
					'<span>'+(time>23?num:time)+':00</span>'
				if(time>23){
					initTime=endT;
					j=0;
					date.setDate(date.getDate()+1)
					html+='<i ></i>'+
						'<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
				}
				html+='</li>'
			}
			
		}
		$(".changeTime ul").html(html)
	}
	
	//绘制24小时底部时间轴
	function drawAxistwentyFour(data){
		var date=new Date();	
		for(var i=0,html="";i<data.length;i++){
			if(data[i].hours%24==0){
				date.setDate(date.getDate()+1)
				html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
					'<span class="upRect"></span>'+
					'<span>'+date.getDate()+'日</span>'
				if(date.getDate()==1){
					date.setDate(date.getDate()+1)
				html+='<i ></i>'+
					'<b class="changeDay">'+(date.getMonth()+1)+'月</b>'
				}
				html+='</li>'
			}
		}
		$(".changeTime ul").html(html)
	}
	//获得1小时降雨时间
	function oneTime(){
		var date=new Date();
		var day=date.getDate();
		var month=date.getMonth()+1;
			// date.setDate(date.getDate()-1)
			date.setHours(date.getHours()-1)
		var hour=date.getHours();

			hour=hour>9?hour:'0'+hour;
			month=month>9?month:'0'+month;
			day=day>9?day:'0'+hour;
			return date.getFullYear()+'/'+month+'/'+day+' '+hour+":00"
	}
	//加载暴雨控件
	function grid_controller(x,productcategory){
		QXMap.removeGridMapProduct();
		QXMap.removeVectorMapProduct();
		x=parseFloat(x).toFixed(1);
		var mapProductOptions = {
	        productcategory: productcategory.type, 
	        time: productcategory.year,
	        regioncode: '330000',
	        method: productcategory.nav,
	        interface: 'getContourEleByTimeAndRegionCode',
	        key: 'zjwlzx',
	        pwd: '123',
	        level: 0,
	        validtime: productcategory.hour, 
	        gridmethod: productcategory.nav,
	        gridinterface: 'getGridForecastEleGridInRectByTimeAndLevelAndValidtime',
	        nlat: 55,
	        slat: 18,
	        wlon: 116,
	        elon: 132,
	        isshowzero: false,
	        isshowcontrol: true,
	        position: "topleft",
	        isforecast:true
	   	};
	    var product = QXMap.addVectorMapProduct(mapProductOptions, function (data) {
	        if (data == null) return;
	        QXMap.showValueClassByType(x);
	    });
	    map.setZoom(9);
	    //map.setCenter({lng:120.260,lat:30.086})
	    console.log(map)
	    // console.log(productcategory)
	};

	$(".overflow ").on("click","li",function(){

		var val=$(this).children("input").prop("checked",true).val()
		console.log(val)
		if(val=="current")oneRain()
		else if(val=="oneHour")oneRain()
		else if(val=="threeHour")threeRain()
		else if(val=="sixHour")sixRain()
		else if(val=="twelveHour")twelveRain()
		else if(val=="twentyfourHour")twentyFourRain()

	})
	$(".btnThreshold").click(function(){
    $(".displayNone").removeClass("displayNone");
	});
	$(".confirmCancel").click(function(){
	    $(".threshold").addClass("displayNone")
	})
})
